import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:get/get_state_manager/src/rx_flutter/rx_obx_widget.dart';
import 'package:mtf/R.dart';
import 'package:mtf/app/data/UsdRecordEntity.dart';
import 'package:mtf/app/modules/property_module/property_controller.dart';
import 'package:mtf/app/theme/app_colors.dart';
import 'package:mtf/app/widget/AppBarLeftTitle.dart';
import 'package:mtf/app/widget/SettingBar.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../utils/AppUtils.dart';
/**
 * GetX Template Generator - fb.com/htngu.99
 * */

class propertyPage extends GetView<propertyController> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarLeftTitle(
        title: "我的资产",
        backgroundColor: c_f33126,
      ),
      body: controller.obx((state) =>  Stack(
      children: [
      Container(
      width: double.infinity,
        height: 200,
        decoration: BoxDecoration(
            borderRadius: const BorderRadius.only(
                bottomRight: Radius.circular(30),
                bottomLeft: Radius.circular(30)),
            gradient: LinearGradient(
                begin: Alignment.topCenter, //渐变开始于上面的中间开始
                end: Alignment.bottomCenter,
                colors: [c_f33126, c_ff5a4c])),
      ),
      RefreshConfiguration(
        headerBuilder: () => ClassicHeader(
            textStyle: TextStyle(color: Colors.white),
            failedIcon: const Icon(Icons.error, color: Colors.white),
            completeIcon: const Icon(Icons.done, color: Colors.white),
            idleIcon: const Icon(Icons.arrow_downward, color: Colors.white),
            releaseIcon: const Icon(Icons.refresh, color: Colors.white)),
        footerBuilder: () => ClassicFooter(),
        child: SmartRefresher(
          enablePullDown: true,
          enablePullUp: false,
          onRefresh: () {
            controller.refreshData();
          },
          controller: controller.refreshController,
          child: Container(
            width: double.infinity,
            child: ListView.builder(
              itemBuilder: (context, index) {
                if (index == 0) {
                  return Padding(
                    padding: EdgeInsets.symmetric(horizontal: 15),
                    child: Obx(() {
                      return Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          SizedBox(
                            height: 15,
                          ),
                          Flex(
                            direction: Axis.horizontal,
                            mainAxisAlignment:
                            MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                "我的资产(USD)",
                                style: TextStyle(
                                    color: Colors.white, fontSize: 13),
                              ),
                              InkWell(
                                onTap: () {
                                  controller.isShow.value =
                                  !controller.isShow.value;
                                },
                                child: Icon(
                                  controller.isShow.value
                                      ? Icons.visibility
                                      : Icons.visibility_off,
                                  size: 15,
                                  color: Colors.white,
                                ),
                              )
                            ],
                          ),
                          SizedBox(
                            height: 10,
                          ),
                          Text(
                            !controller.isShow.value
                                ? "****"
                                : controller.account.value,
                            style: TextStyle(
                                fontSize: 26,
                                fontWeight: FontWeight.w500,
                                color: Colors.white),
                          ),
                          // SizedBox(height: 3),
                          // Text(
                          //   "≈4785825.64CNY",
                          //   style: TextStyle(
                          //       fontSize: 13, color: Colors.white),
                          // ),
                          SizedBox(height: 15),
                          Row(
                            children: [
                              Expanded(
                                flex: 1,
                                child: Column(
                                  children: [
                                    Text(
                                      controller.Profit_sum.value,
                                      style: TextStyle(
                                          fontSize: 16,
                                          color: Colors.white),
                                    ),
                                    SizedBox(height: 3),
                                    Text(
                                      "收益总计(USD)",
                                      style: TextStyle(
                                          fontSize: 12,
                                          color: Colors.white),
                                    )
                                  ],
                                ),
                              ),
                              Expanded(
                                flex: 1,
                                child: Column(
                                  children: [
                                    Text(
                                      controller.Storage_sum.value,
                                      style: TextStyle(
                                          fontSize: 16,
                                          color: Colors.white),
                                    ),
                                    SizedBox(height: 3),
                                    Text(
                                      "利息总计(USD)",
                                      style: TextStyle(
                                          fontSize: 12,
                                          color: Colors.white),
                                    )
                                  ],
                                ),
                              ),
                              // Expanded(
                              //   flex: 1,
                              //   child: Column(
                              //     children: [
                              //       Text(
                              //         "39.80",
                              //         style: TextStyle(
                              //             fontSize: 16,
                              //             color: Colors.white),
                              //       ),
                              //       SizedBox(height: 3),
                              //       Text(
                              //         "累计收益(USD)",
                              //         style: TextStyle(
                              //             fontSize: 12,
                              //             color: Colors.white),
                              //       )
                              //     ],
                              //   ),
                              // )
                            ],
                          ),
                          SizedBox(height: 15),
                          Divider(
                            height: 1,
                            color: Colors.white38,
                          ),
                          Flex(
                            direction: Axis.horizontal,
                            mainAxisAlignment:
                            MainAxisAlignment.spaceAround,
                            children: [
                              InkWell(
                                onTap: () {
                                  controller.clickRechargeRecord();
                                },
                                child: Padding(
                                  padding:
                                  EdgeInsets.symmetric(vertical: 10),
                                  child: Text("充值记录 >",
                                      style: TextStyle(
                                          color: Colors.white,
                                          fontSize: 12)),
                                ),
                              ),
                              InkWell(
                                onTap: () {
                                  controller.clickWithdrawRecord();
                                },
                                child: Padding(
                                  padding:
                                  EdgeInsets.symmetric(vertical: 10),
                                  child: Text("提现记录 >",
                                      style: TextStyle(
                                          color: Colors.white,
                                          fontSize: 12)),
                                ),
                              ),
                              InkWell(
                                onTap: () {
                                  controller.clickBankManager();
                                },
                                child: Padding(
                                  padding:
                                  EdgeInsets.symmetric(vertical: 10),
                                  child: Text("银行卡管理 >",
                                      style: TextStyle(
                                          color: Colors.white,
                                          fontSize: 12)),
                                ),
                              )
                            ],
                          )
                        ],
                      );
                    }),
                  );
                } else {
                  return Container(
                    margin: EdgeInsets.symmetric(horizontal: 10),
                    width: double.infinity,
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(10)),
                    child: Column(
                      children: [
                        SizedBox(
                          height: 15,
                        ),
                        Flex(
                          direction: Axis.horizontal,
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: [
                            InkWell(
                              onTap: () {
                                controller.clickRecharge();
                              },
                              child: Column(
                                children: [
                                  SvgPicture.asset(
                                    R.assetsImgIcSvgRecharge,
                                  ),
                                  SizedBox(
                                    height: 3,
                                  ),
                                  Text(
                                    "充值",
                                    style: TextStyle(
                                        color: c_333333, fontSize: 13),
                                  )
                                ],
                              ),
                            ),
                            InkWell(
                              onTap: () {
                                controller.clickWithdrawal();
                              },
                              child: Column(
                                children: [
                                  SvgPicture.asset(
                                    R.assetsImgIcSvgWithdrawal,
                                  ),
                                  SizedBox(
                                    height: 3,
                                  ),
                                  Text(
                                    "提款",
                                    style: TextStyle(
                                        color: c_333333, fontSize: 13),
                                  )
                                ],
                              ),
                            ),
                            // InkWell(
                            //   onTap: () {
                            //     controller.clickCover();
                            //   },
                            //   child: Column(
                            //     children: [
                            //       SvgPicture.asset(
                            //         R.assetsImgIcSvgConvert,
                            //       ),
                            //       SizedBox(
                            //         height: 3,
                            //       ),
                            //       Text(
                            //         "划转",
                            //         style: TextStyle(
                            //             color: c_333333, fontSize: 13),
                            //       )
                            //     ],
                            //   ),
                            // )
                          ],
                        ),
                        SizedBox(
                          height: 15,
                        ),
                        Padding(
                          padding:
                          const EdgeInsets.symmetric(horizontal: 10),
                          child: Flex(
                            direction: Axis.horizontal,
                            mainAxisAlignment:
                            MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                "USD明细",
                                style: TextStyle(
                                    fontSize: 16,
                                    color: c_333333,
                                    fontWeight: FontWeight.w500),
                              ),
                              InkWell(
                                onTap: () {
                                  controller.clickUsd();
                                },
                                child: Text(
                                  "更多>",
                                  style: TextStyle(
                                      fontSize: 13, color: c_999999),
                                ),
                              ),
                            ],
                          ),
                        ),
                        Obx(() {
                          return ListView.separated(
                            itemBuilder: (context, index) {
                              return itemChange(controller.usdList[index]);
                            },
                            separatorBuilder: (context, index) {
                              return Divider(
                                height: 1,
                              );
                            },
                            itemCount: controller.usdList.length,
                            physics: NeverScrollableScrollPhysics(),
                            shrinkWrap: true,
                          );
                        })
                      ],
                    ),
                  );
                }
              },
              shrinkWrap: true,
              itemCount: 2,
              physics: NeverScrollableScrollPhysics(),
            ),
          ),
        ),
      )
      ],
    )),
    );
  }

  Widget orderItem(ListUSDElement bean) {
    var infoMap = bean.infoMap();
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 10, vertical: 15),
      width: double.infinity,
      child: Column(
        children: [
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 10),
            child: Flex(
              direction: Axis.horizontal,
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  "序号：${bean.num.toString() ?? ""}",
                  style: TextStyle(color: c_333333, fontSize: 15),
                ),
                Text(
                  "订单号：${bean.order ?? ""}",
                  style: TextStyle(color: c_333333, fontSize: 15),
                ),
              ],
            ),
          ),
          SizedBox(
            height: 5,
          ),
          GridView.builder(
            padding: EdgeInsets.symmetric(vertical: 10),
            physics: NeverScrollableScrollPhysics(),
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisSpacing: 10.0, //水平子 Widget 之间间距
              crossAxisCount: 3, //一行的 Widget 数量
              childAspectRatio: 2,
              mainAxisSpacing: 10,
            ),
            itemBuilder: (context, index) {
              return Flex(
                direction: Axis.vertical,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Text(infoMap[index].valueTxt ?? ""),
                  SizedBox(
                    height: 3,
                  ),
                  Text(infoMap[index].keyTxt ?? ""),
                ],
              );
            },
            shrinkWrap: true,
            itemCount: infoMap.length,
          ),
          Align(
            alignment: Alignment.centerRight,
            child: Text(
              bean.timeAt.toString(),
              style: TextStyle(color: c_999999, fontSize: 12),
            ),
          )
        ],
      ),
    );
  }

  Widget itemChange(ListUSDElement bean) {
    return Container(
      margin: EdgeInsets.only(bottom: 10, left: 10, right: 10),
      padding: EdgeInsets.only(bottom: 15),
      decoration: BoxDecoration(
          color: Colors.white, borderRadius: BorderRadius.circular(10)),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SettingBar(
              leftText: bean.actionTxt ?? "",
              paddingHorizontal: 0,
              leftIconSpace: 0,
              rightIconSpace: 0,
              rightText: "订单号：${bean.order ?? ""}",
              rightTextColor: c_666666,
              rightTextSize: 12,
              backgroupColor: Colors.transparent,
              showDivider: true),
          SizedBox(
            height: 15,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                "交易品种",
                style: TextStyle(color: c_999999, fontSize: 12),
              ),
              Text(
                "交易量(手数)",
                style: TextStyle(color: c_999999, fontSize: 12),
              ),
              Text(
                "交易价格(USD)",
                style: TextStyle(color: c_999999, fontSize: 12),
              ),
            ],
          ),
          SizedBox(
            height: 5,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                bean.symbol ?? "",
                style: TextStyle(color: c_333333, fontSize: 14),
              ),
              Text(
                bean.volumeTxt.toString() ?? "",
                style: TextStyle(color: c_333333, fontSize: 14),
              ),
              Text(
                bean.price ?? "",
                style: TextStyle(color: c_333333, fontSize: 14),
              ),
            ],
          ),
          SizedBox(
            height: 10,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                "手续费(USD)",
                style: TextStyle(color: c_999999, fontSize: 12),
              ),
              Text(
                "隔夜利息(USD)",
                style: TextStyle(color: c_999999, fontSize: 12),
              ),
              Text(
                "盈亏(USD)",
                style: TextStyle(color: c_999999, fontSize: 12),
              ),
            ],
          ),
          SizedBox(
            height: 5,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                bean.priceTp ?? "",
                style: TextStyle(color: c_333333, fontSize: 14),
              ),
              Text(
                bean.storage ?? "",
                style: TextStyle(color: c_333333, fontSize: 14),
              ),
              Text(
                bean.profit ?? "",
                style: TextStyle(color: c_333333, fontSize: 14),
              ),
            ],
          ),
          SizedBox(
            height: 10,
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                "时间",
                style: TextStyle(color: c_999999, fontSize: 12),
              ),
              SizedBox(
                height: 5,
              ),
              Text(
                AppUtils.dateToString(bean.timeAt),
                style: TextStyle(color: c_333333, fontSize: 14),
              ),
            ],
          )
        ],
      ),
    );
  }
}
